import { JSEncrypt } from "jsencrypt"
function randomStr(length: number) {
    const x = "0123456789qwertyuioplkjhgfdsazxcvbnm";
    let tmp = "";
    for (let i = 0; i < length; i++) {
        tmp += x.charAt(Math.ceil(Math.random() * 100000000) % x.length);
    }
    return tmp;
}

function makeSignature(nonceStr: string, timestamp: any) {
    const crypt = new JSEncrypt();
    crypt.setPublicKey(`MIGeMA0GCSqGSIb3DQEBAQUAA4GMADCBiAKBgHT/ZkPTGeihonUzWZ38UyKMhpNc
  /Leg1WN1Rk6IK2AUBOcqXRozbfuQ7xfkGV1JTpWJVQS0Wz9G1a2MjX1ErOgVn2ga
  jJ/wMO9tDyaOG2nd5U/O+lHgCatTVpIZ0o3DIcwsgUxbREOsjssDjafN2+U6yBeg
  Mj8CwfsZsV47MpJzAgMBAAE=`);
    const param = {
        timestamp,
        nonceStr,
        key: "xinanyuansu6688"
    };
    let str = JSON.stringify(param);
    return crypt.encrypt(str);
}



export function getSignatureParam() {
    const nonceStr = randomStr(8);
    const date: any = new Date();
    const timestamp = Date.parse(date) / 1000;
    const signature = makeSignature(nonceStr, timestamp);
    return {
        signature: signature,
        timestamp: timestamp,
        nonceStr: nonceStr
        // sessionKey: cache.get('sessionKey')
    };
}


